package org.java.mathgame.screens;


import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import org.java.mathgame.MathGame;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.ArrayList;


public class MainMenuScreen implements Screen {
    private MathGame game;
    private SpriteBatch spriteBatch;
    private BitmapFont font;
    private Stage stage;
    private Stage dialogStage;
    Table dialogTable;
    TextButton resumeButton;
    TextButton newGameButton;
    TextButton highScoreButton;
    TextButton htpButton;

    // constructor to keep a reference to the main Game class
    public MainMenuScreen(final MathGame game){
        int gdxWidth = Gdx.graphics.getWidth();
        int gdxHeight = Gdx.graphics.getHeight();
        this.game = game;
        ArrayList<TextButton.TextButtonStyle> buttonStyles = new ArrayList<TextButton.TextButtonStyle>();
        spriteBatch = new SpriteBatch();
        font = new BitmapFont();
        stage = new Stage(gdxWidth,gdxHeight,true,spriteBatch);
        dialogStage = new Stage(gdxWidth,gdxHeight,true,spriteBatch);
        Table rootTable = new Table();
        rootTable.setFillParent(true);
        rootTable.setBackground(new ButtonBackground(new Color(238 / 255f, 238 / 255f, 238 / 255f, 1)));
        Table menuTable = new Table();
        TextButton.TextButtonStyle gridButtonStyle = new TextButton.TextButtonStyle();
        TextButton.TextButtonStyle operatorButtonStyle;
        Color[] colors = new Color[]{
                new Color(210 / 255f, 57 / 255f, 59 / 255f, 1),
                new Color(58 / 255f, 143 / 255f, 208 / 255f, 1),
                new Color(59 / 255f, 209 / 255f, 112 / 255f, 1),
                new Color(208 / 255f, 147 / 255f, 58 / 255f, 1)};
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        BitmapFont buttonFont;
        if (Gdx.graphics.getWidth() > 700) {
            buttonFont = generator.generateFont((int) (Gdx.graphics.getHeight() / 25f));
            buttonFont.scale(1.2f);
        } else
            buttonFont = generator.generateFont((int) (Gdx.graphics.getHeight() / 12.5f));
        //generator.dispose();
        for (int i = 0; i < 4; i++) {
            TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
            style.font = buttonFont;
            style.pressedOffsetX = 1;
            style.pressedOffsetY = -1;
            style.font.setColor(Color.WHITE);
            style.up = new ButtonBackground(colors[i]);
            buttonStyles.add(style);
        }
        TextButton.TextButtonStyle infoStyle = new TextButton.TextButtonStyle();
        infoStyle.up = new ButtonBackground(new Color(35/255f,35/255f,35/255f,1));
        infoStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));

        Label.LabelStyle backStyle = new Label.LabelStyle();
        backStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));
        backStyle.background = new ButtonBackground(new Color(0,0,0,0));

        Label backLabel = new Label("  ...",backStyle);
        TextButton backButton = new TextButton("Brain Crunch ",infoStyle);

        backButton.getLabel().remove();
        backButton.clear();
        backButton.add(backLabel).expandX().left();
        backButton.add(backButton.getLabel());
        backButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                aboutDialog();
            }
        });

        resumeButton = new TextButton("Resume",buttonStyles.get(0));
        newGameButton = new TextButton("New Game",buttonStyles.get(1));
        highScoreButton = new TextButton("High Scores",buttonStyles.get(2));
        htpButton = new TextButton("How To Play",buttonStyles.get(3));
        resumeButton.clear();
        resumeButton.add(resumeButton.getLabel()).expandX().left();
        newGameButton.clear();
        newGameButton.add(newGameButton.getLabel()).expandX().left();
        highScoreButton.clear();
        highScoreButton.add(highScoreButton.getLabel()).expandX().left();
        htpButton.clear();
        htpButton.add(htpButton.getLabel()).expandX().left();

        Texture playTexture = new Texture("data/play.png");
        Texture newTexture = new Texture("data/new.png");
        Texture starTexture = new Texture("data/star.png");
        Texture helpTexture = new Texture("data/help.png");
        resumeButton.add(new ImageButton(new SpriteDrawable(new Sprite(playTexture)))).height(resumeButton.getHeight()).pad(5);
        newGameButton.add(new ImageButton(new SpriteDrawable(new Sprite(newTexture)))).height(newGameButton.getHeight()).pad(5);
        highScoreButton.add(new ImageButton(new SpriteDrawable(new Sprite(starTexture)))).height(resumeButton.getHeight()).pad(5);
        htpButton.add(new ImageButton(new SpriteDrawable(new Sprite(helpTexture)))).height(newGameButton.getHeight()).pad(5);;
        resumeButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
                return true;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button){
                game.setScreen(game.gameScreen);
            }
        });

        newGameButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                game.setScreen(game.gameSettingsScreen);
            }
        });

        highScoreButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                game.highScoreScreen = new HighScoreScreen(game);
                game.setScreen(game.highScoreScreen);
            }
        });

        htpButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                game.setScreen(game.howToPlayScreen);
            }
        });
        rootTable.pad(10);
        rootTable.setFillParent(true);
        rootTable.add(backButton).expandX().fillX().top();
        rootTable.row();
        menuTable.add(resumeButton).uniform().expandX().fillX().spaceBottom(20);
        menuTable.row();
        menuTable.add(newGameButton).uniform().expandX().fillX().spaceBottom(20);
        menuTable.row();
        menuTable.add(highScoreButton).uniform().expandX().fillX().spaceBottom(20);
        menuTable.row();
        menuTable.add(htpButton).uniform().expandX().fillX();
        rootTable.add(menuTable).expandX().fillX().expandY();//.width(Gdx.graphics.getWidth()/2f).height(Gdx.graphics.getHeight()/2f);
        stage.addActor(rootTable);

        dialogTable = new Table();
        dialogTable.setWidth(Gdx.graphics.getWidth()*(4/5f));
        dialogTable.setHeight(Gdx.graphics.getHeight()/3f);
        dialogTable.setX((gdxWidth - dialogTable.getWidth()) / 2f);
        dialogTable.setY(gdxHeight / 2f);
        dialogTable.setBackground(new ButtonBackground(new Color(205 / 255f, 205 / 255f, 205 / 255f, 1)));


        Label.LabelStyle labelStyle = new Label.LabelStyle();
        labelStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/35f));
        labelStyle.fontColor = Color.BLACK;
        TextButton titleButton = new TextButton("About:",infoStyle);
        TextButton.TextButtonStyle okButtonStyle = new TextButton.TextButtonStyle();
        okButtonStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/20f),"OK",false);
        okButtonStyle.up = new ButtonBackground(new Color(162 / 255f, 9 / 255f, 11 / 255f, 1));
        TextButton okButton = new TextButton("OK",okButtonStyle);
        okButton.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                dialogStage.clear();
                Gdx.input.setInputProcessor(stage);
            }
        });
        dialogTable.add(titleButton).expandX().fillX().expandY().top();
        dialogTable.row();
        dialogTable.add(new Label("Made by:",labelStyle)).expandX().left().expandY().top();
        dialogTable.row();
        dialogTable.add(new Label("Arturs Liepins",labelStyle)).right().expandY();
        dialogTable.row();
        dialogTable.add(new Label("Roberts Bergmanis",labelStyle)).right().expandY();
        dialogTable.row();
        dialogTable.add(new Label("Martins Rutkovskis",labelStyle)).right().expandY();
        dialogTable.row();
        dialogTable.add(new Label("Vadims Vorobjovs",labelStyle)).right().expandY();
        dialogTable.row();
        dialogTable.add(new Label(" ",labelStyle)).right().expandY();
        dialogTable.row();
        dialogTable.add(okButton.pad(5f));
        dialogTable.pad(7.5f);

        Gdx.input.setInputProcessor(stage);
        generator.dispose();
    }

    public void aboutDialog(){
        dialogStage.addActor(dialogTable);

        dialogTable.debug();
        Gdx.input.setInputProcessor(dialogStage);
    }

    @Override
    public void render(float delta) {
        if (!game.isGameRunning())
            resumeButton.setVisible(false);
        else
            resumeButton.setVisible(true);
        Gdx.gl.glClearColor(0.5f, 0.3f, 0.6f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//        font.setColor(1f, 1f, 1f, 1f);
        spriteBatch.begin();
//        font.draw(spriteBatch, "mainMenuScreen", Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);

        Table.drawDebug(dialogStage);
        spriteBatch.end();
        stage.draw();
        dialogStage.draw();
        //stage.draw();
        //if (Gdx.input.justTouched()) // use your own criterion here
        //    game.setScreen(game.howToPlayScreen);
    }


    @Override
    public void resize(int width, int height) {
    }


    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
        //game.highScoreScreen = new HighScoreScreen(game);
    }


    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }


    @Override
    public void pause() {
    }


    @Override
    public void resume() {
    }


    @Override
    public void dispose() {
        // never called automatically
    }
}
